package com.grantha.controllers;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.grantha.util.DB;
import com.grantha.util.MD5Generator;
import com.grantha.util.Util;
import java.sql.*;
import com.grantha.util.MD5Generator;
import com.grantha.util.Util;
/**
 * Servlet implementation class MAddUserController
 */
@WebServlet("/MAddUserController")
public class MAddUserController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger log = Logger.getLogger(this.getClass());
    /**
     *  @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
				
	}
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		
		log.info("Servlet Stated");
		//coding start 
		
		String username = request.getParameter("namefield"); 
		
//		ResultSet rs = DB.search("select username from users where username = ?", username);
//		
//		if(rs.next())
//			out.print(Util.sendJSONMessage(false, "username already taken"));
//		
		
		String password = MD5Generator.md5(request.getParameter("password1"));
		String password2 = MD5Generator.md5(request.getParameter("password2"));
		String rights = request.getParameter("rights");
		
		log.info("username="+username);
		log.info("password="+password);
		log.info("rights="+rights);
		
		
		if((username.equals(""))||(password.equals("d41d8cd98f00b204e9800998ecf8427e"))||(rights.equals(""))){
					out.print(Util.sendJSONMessage(false, "All required fields must be filled out"));
					
				}
		else if(!password.equals(password2)){
			out.print(Util.sendJSONMessage(false, "Passwords doesn't match"));
		}
		//
		else{
			try {
				ResultSet rs = DB.search("select username from users where username = ?", username);
				if(rs.next()){
					out.print(Util.sendJSONMessage(false, "username already taken"));
				}
				else{
					DB.update("insert into users (username, password, rights) values (?, ?, ?)", username, password, rights );
					out.print(Util.sendJSONMessage(true, "User added successfully"));
					log.info("success");
				}
			} catch (Exception e) {
//			out.print("error");
			out.print(Util.sendJSONMessage(false, "Error when adding user"));
			log.error("Error");
		}
				}
		//coding ends
		
		out.close();
		log.info("Servlet end");
	}

	
}
